#include "queue.h"

struct Queue* init()
{
    struct Queue *qe=NULL;
    qe = (struct Queue*)malloc(sizeof(struct Queue));
    if(qe == NULL)
    {
        printf("create queue malloc error \n");
        return NULL;
    }

    qe->head = (struct Node*)malloc(sizeof(struct Node));
    if(qe->head == NULL)
    {
        free(qe);
        printf("create node malloc error\n");
        return NULL;
    }
    qe->head->next = NULL;
    qe->head->data = 0;
    qe->tail = qe->head;
    return qe;
}
//入队
int in(struct Queue *qu, int data)
{
    if(qu == NULL)
    {
        return -1;
    }
    struct Node *pnew = NULL;
    pnew = (struct Node*)malloc(sizeof(struct Node));
    if(pnew == NULL)
    {
        printf("in pnew malloc is fail\n");
        return -1;
    }
    pnew->data = data;
    pnew->next = NULL;
    qu->tail->next = pnew;
    qu->tail = qu->tail->next;
    return 1;
}

int is_empty_linkqueue(struct Queue *qe)
{
    if(qe == NULL)
    {
        return -1;
    }
    return ((qe->head == qe->tail) ? 1 : 0);
}

//出队
int out(struct Queue *qe)
{
    int ret = 0;
    if(qe == NULL)
    {
        return -1;
    }
    if(is_empty_linkqueue(qe) == 1)
    {
        return -2;
    }
    struct Node *pdel = NULL;
    if(qe->head->next == NULL) 
    {
        qe->tail = qe->head;
        return -2;
    }
    pdel = qe->head->next;
    qe->head->next = pdel->next;
    ret = pdel->data; 
    free(pdel);
    pdel = NULL;
    return ret;
}